from wx import wx
import Image
import StringIO
import base64
import sqlite3

class ResizeImageDialog(wx.Dialog):
    def __init__(self, parent, ID, title, size=wx.DefaultSize, pos=wx.DefaultPosition, style=wx.DEFAULT_DIALOG_STYLE, useMetal=False,):
        # Instead of calling wx.Dialog.__init__ we precreate the dialog
        # so we can set an extra style that must be set before
        # creation, and then we create the GUI object using the Create
        # method.
        pre = wx.PreDialog()
        pre.SetExtraStyle(wx.DIALOG_EX_CONTEXTHELP)
        pre.Create(parent, ID, title, pos, size, style)

        # This next step is the most important, it turns this Python
        # object into the real wrapper of the dialog (instead of pre)
        # as far as the wxPython extension is concerned.
        self.PostCreate(pre)

        # This extra style can be set after the UI object has been created.
        if 'wxMac' in wx.PlatformInfo and useMetal:
            self.SetExtraStyle(wx.DIALOG_EX_METAL)


        # Now continue with the normal construction of the dialog
        # contents
        self.inString = StringIO.StringIO()
        tmpString = self.GetParent().imageListBox.GetStringSelection()
        tmpString2 = tmpString.split(".", 1)
        self.data = base64.decodestring(self.GetParent().TopLevelParent.currentImageDict[int(tmpString2[0])][0])
        self.inString.write(self.data)
        self.inString.seek(0)
        self.originalImage = Image.open(self.inString)
        self.inString.close()
        
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        topBox = wx.BoxSizer(wx.HORIZONTAL)
        leftBox = wx.BoxSizer(wx.VERTICAL)
        tBox = wx.GridSizer(1, 5, 1, 1)
        bBox = wx.GridSizer(1, 5, 1, 1)
        botBox = wx.BoxSizer(wx.HORIZONTAL)        
        self.widthSpinnerPX = wx.SpinCtrl(self, -1, size=(50, 20))
        self.heightSpinnerPX = wx.SpinCtrl(self, -1, size=(50, 20))
        self.widthSpinnerPT = wx.SpinCtrl(self, -1, size=(50, 20))
        self.heightSpinnerPT = wx.SpinCtrl(self, -1, size=(50, 20))
        self.previewPanel = wx.Panel(self)
        self.lText = wx.StaticText(self, -1, "Width: ")
        self.rText = wx.StaticText(self, -1, "Height: ")
        self.pText1 = wx.StaticText(self, -1, " px ")
        self.pText2 = wx.StaticText(self, -1, " px ")
        self.pText3 = wx.StaticText(self, -1, " % ")
        self.pText4 = wx.StaticText(self, -1, " % ")
        
        self.previewPanel.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW))
        
        self.DrawPreviewImage(self.originalImage.size[0], self.originalImage.size[1])
        
        self.widthSpinnerPX.SetRange(0, self.originalImage.size[0])
        self.heightSpinnerPX.SetRange(0, self.originalImage.size[1])
        self.widthSpinnerPT.SetRange(0, 100)
        self.heightSpinnerPT.SetRange(0, 100)
        
        self.widthSpinnerPT.SetValue(100)
        self.heightSpinnerPT.SetValue(100)
        self.widthSpinnerPX.SetValue(self.originalImage.size[0])
        self.heightSpinnerPX.SetValue(self.originalImage.size[1])
        
        wx.EVT_SPINCTRL(self, self.widthSpinnerPX.GetId(), self.OnWidthSpinPX)
        wx.EVT_SPINCTRL(self, self.heightSpinnerPX.GetId(), self.OnHeightSpinPX)
        wx.EVT_SPINCTRL(self, self.widthSpinnerPT.GetId(), self.OnWidthSpinPT)
        wx.EVT_SPINCTRL(self, self.heightSpinnerPT.GetId(), self.OnHeightSpinPT)
        
        
        topStaticBox = wx.StaticBox(self, -1, "Resize")
        topStaticBoxSizer = wx.StaticBoxSizer(topStaticBox)
        bottomStaticBox = wx.StaticBox(self, -1, "Preview")
        bottomStaticBoxSizer = wx.StaticBoxSizer(bottomStaticBox)
        
        tBox.Add(self.lText, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL)
        tBox.Add(self.widthSpinnerPX, 0, wx.ALL)
        tBox.Add(self.pText1, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL)
        tBox.Add(self.widthSpinnerPT, 0, wx.ALL)
        tBox.Add(self.pText3, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL)
        bBox.Add(self.rText, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL)
        bBox.Add(self.heightSpinnerPX, 0, wx.ALL)
        bBox.Add(self.pText2, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL)
        bBox.Add(self.heightSpinnerPT, 0, wx.ALL)
        bBox.Add(self.pText4, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL)
        leftBox.Add(tBox, 0, wx.ALL)
        leftBox.Add(bBox, 0, wx.ALL)
        topBox.Add(leftBox, 0, wx.ALL)
        botBox.Add(self.previewPanel, 1, wx.EXPAND)
        topStaticBoxSizer.Add(topBox, 0, wx.ALL)
        bottomStaticBoxSizer.Add(botBox, 0, wx.ALL)
        self.sizer.Add(topStaticBoxSizer, 0, wx.ALL)
        self.sizer.Add(bottomStaticBoxSizer, 1, wx.EXPAND)
        
        line = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL)
        self.sizer.Add(line, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.TOP, 5)

        btnsizer = wx.StdDialogButtonSizer()
        
        if wx.Platform != "__WXMSW__":
            btn = wx.ContextHelpButton(self)
            btnsizer.AddButton(btn)
        
        btn = wx.Button(self, wx.ID_OK)
        btn.SetHelpText("The OK button completes the dialog")
        btn.SetLabel("Resize")
        btn.SetDefault()
        btnsizer.AddButton(btn)
        wx.EVT_BUTTON(self, btn.GetId(), self.OnResize)

        btn = wx.Button(self, wx.ID_CANCEL)
        btn.SetHelpText("The Cancel button cancels the dialog.")
        btnsizer.AddButton(btn)
        btnsizer.Realize()

        self.sizer.Add(btnsizer, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

        self.SetSizer(self.sizer)
        self.sizer.Fit(self)

    def OnResize(self, event):
        tmpString = self.GetParent().imageListBox.GetStringSelection()
        tmpString2 = tmpString.split(".", 1)
        self.outString = StringIO.StringIO()
        self.resizeImage.save(self.outString, "PNG")
        con = sqlite3.connect(self.TopLevelParent.database)
        cur = con.cursor()
        cur.execute("UPDATE ResourceTable set resourceData = (?) where resourceID = (?)", (base64.encodestring(sqlite3.Binary(self.outString.getvalue())), int(tmpString2[0])))
        con.commit()
        con.close()
        self.GetParent().ShowBitmapFromPIL(self.GetParent().imageViewer, self.resizeImage)
        self.outString.close()
        self.Close()
        
    def OnWidthSpinPX(self, event):
        self.widthSpinnerPT.SetValue(int((float(self.widthSpinnerPX.GetValue())/float(self.originalImage.size[0]))*100))
        self.DrawPreviewImage(self.widthSpinnerPX.GetValue(), self.heightSpinnerPX.GetValue())
        
    def OnWidthSpinPT(self, event):
        self.widthSpinnerPX.SetValue(int((float(self.widthSpinnerPT.GetValue())/100)*float(self.originalImage.size[0])))
        self.DrawPreviewImage(self.widthSpinnerPX.GetValue(), self.heightSpinnerPX.GetValue())
        
    def OnHeightSpinPX(self, event):
        self.heightSpinnerPT.SetValue(int((float(self.heightSpinnerPX.GetValue())/float(self.originalImage.size[1]))*100))
        self.DrawPreviewImage(self.widthSpinnerPX.GetValue(), self.heightSpinnerPX.GetValue())
        
    def OnHeightSpinPT(self, event):
        self.heightSpinnerPX.SetValue(int((float(self.heightSpinnerPT.GetValue())/100)*float(self.originalImage.size[1])))
        self.DrawPreviewImage(self.widthSpinnerPX.GetValue(), self.heightSpinnerPX.GetValue())
        
    def DrawPreviewImage(self, newWidth, newHeight):
        self.inString = StringIO.StringIO()
        tmpString = self.GetParent().imageListBox.GetStringSelection()
        tmpString2 = tmpString.split(".", 1)
        self.data = base64.decodestring(self.GetParent().TopLevelParent.currentImageDict[int(tmpString2[0])][0])
        self.inString.write(self.data)
        self.inString.seek(0)
        self.displayImage = Image.open(self.inString)
        self.resizeImage = self.displayImage.resize((newWidth, newHeight), Image.ANTIALIAS)
        self.GetParent().ShowBitmapFromPIL(self.previewPanel, self.resizeImage)
        self.sizer.Fit(self)